// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    data: {
        info: [],
        khid: '',
        nowDate: new Date().getTime(),
        orderDate: '',
        detail: {},
        total: 0,
        count: 0,
        dateField: '',
        workshopOption: {},
        lineOption: {},
        typeOption: [],
        materialOption: {},
        unitOption: [],
        bomOption: {},
        numOption: {}
    },
    // 选择生产线
    getLine: async function (field, workshop = '') {
        let res = await api.curlPost('?cla=inventoryOther&fun=line', {workshop: workshop})
        if (res.code === 1) {
            this.setData({
                [field]: res.data
            })
        }
    },
    getMachiningType: async function () {
        let res = await api.curlGet('?cla=inventoryOther&fun=machiningType')
        if (res.code === 1) {
            this.setData({
                typeOption: res.data
            })
        }
    },
    main: async function () {
        await this.getLine('workshopOption')
        await this.getMachiningType()
    },
    // 更新本地数据
    getDetail: async function (id) {
        let res = await api.curlPost('?cla=machiningOrder&fun=detail', {id: id})
        if (res.code === 1) {
            this.setData({
                info: res.data.result.info,
                detail: res.data.result.material,
            })
            wx.setStorageSync('machiningOrderDetailData', res.data.result.material)
        }
    },
    assign: async function (data) {
        const obj = api.jsonToArray(data)
        this.setData({
            info: obj,
            detail: obj.problem
        })
    },
    onLoad: async function (options) {
        let that = this,
            id = options.id ? options.id : ''
        if (id) {
            await that.getDetail(id)
        }else{
            wx.setStorageSync('machiningOrderDetailData', {})
        }
        await that.main()
    },
    onShow: async function () {
        const problem = wx.getStorageSync('machiningOrderDetailData')
        if (problem) {
            this.setData({
                detail: problem
            })
        }
    },
    onDisplay: function (e) {
        let obj = e.currentTarget.dataset
        this.setData({
            calendar: true,
            dateField: obj.field
        })
    },
    // 关闭
    onClose: function (e) {
        let obj = e.currentTarget.dataset
        if (obj) {
            this.setData({[obj.target]: false})
        }
    },
    goto(e) {
        return api.goto(e)
    },
    funRouting: function (type, detail) {
        const array = detail.value
        switch (type) {
            // 选项与值相等
            case 'workshop':
                this.setData({
                    lineOption: this.getLine('lineOption', array['text'])
                })
                return array['text']
            // 选项与值为key value
            case 'lineName':
                this.setData({
                    ['info.lineId']: array['value']
                })
                return array['text']
            case 'staffName':
                this.setData({
                    ['info.manager']: array['value']
                })
                return array['text']
            // 日期
            case 'startDate':
            case 'endDate':
                return api.formatDate(detail)
            // 单字符串
            default:
                return detail.value
        }
    },
    // 确认选择
    onConfirm: function (e) {
        let obj = e.currentTarget.dataset
        let field = 'info.' + obj.field
        this.setData({
            [obj.target]: false,
            [field]: this.funRouting(obj.field, e.detail),
        })
    },
    showPopup: function (obj) {
        let popup = obj.currentTarget.dataset.show
        if (popup) {
            this.setData({
                [popup]: true
            });
        }
    },
    delRow: function (obj) {
        const id = obj.currentTarget.dataset.id
        let array = this.data.detail
        delete array[id]
        this.setData({
            detail: array
        })
        wx.setStorageSync('machiningOrderDetailData', array)
    },
    formSubmit: async function (e) {
        const machiningOrderDetailData = wx.getStorageSync('machiningOrderDetailData')
        let obj = e.detail.value
        if (obj) {
            obj['detail'] = api.arrayToJson(machiningOrderDetailData)
            let res = await api.curlPost('?cla=machiningOrder&fun=edit', obj)
            if (res.code === 1) {
                wx.navigateTo({
                    url: 'detail?id=' + res.data
                })
                wx.removeStorageSync('machiningOrderDetailData')
                api.showToast('新增成功', 'success')
            } else {
                api.showToast(res.warn)
            }
        }
    }
});